package com.opages.platform.common.config.basic;



import com.opages.platform.common.utils.DurationUtil;

import java.time.Duration;

public abstract class AbstractConfiguration implements Configuration {
    @Override
    public short getShort(String dataId, short defaultValue) {
        String result = getConfig(dataId, String.valueOf(defaultValue));
        return Short.parseShort(result);
    }

    @Override
    public short getShort(String dataId) {
        return getShort(dataId, (short) 0);
    }

    @Override
    public int getInt(String dataId, int defaultValue) {
        String result = getConfig(dataId, String.valueOf(defaultValue));
        return Integer.parseInt(result);
    }

    @Override
    public int getInt(String dataId) {
        return getInt(dataId, 0);
    }


    @Override
    public long getLong(String dataId, long defaultValue) {
        String result = getConfig(dataId, String.valueOf(defaultValue));
        return Long.parseLong(result);
    }

    @Override
    public long getLong(String dataId) {
        return getLong(dataId, 0L);
    }

    @Override
    public Duration getDuration(String dataId) {
        return getDuration(dataId, Duration.ZERO);
    }

    @Override
    public Duration getDuration(String dataId, Duration defaultValue) {
        String result = getConfig(dataId, defaultValue.toMillis() + "ms");
        return DurationUtil.parse(result);
    }



    @Override
    public boolean getBoolean(String dataId, boolean defaultValue) {
        String result = getConfig(dataId, String.valueOf(defaultValue));
        return Boolean.parseBoolean(result);
    }

    @Override
    public boolean getBoolean(String dataId) {
        return getBoolean(dataId, false);
    }


    @Override
    public String getConfig(String dataId) {
        return getConfig(dataId, null);
    }

    /**
     * Gets type name.
     *
     * @return the type name
     */
    public abstract String getTypeName();
}
